package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.NewsDesActivity;
import com.winz.cultureapp.adapter.xrcv.ShowVolunteerAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ShowVolunteerBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.MarginDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 文化志愿服务 -- 后台添加模块
 */
public class ShowVolunteerFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_show)
    public XRecyclerView mXrcv_show;

    private boolean isRefresh = true;
    private int limit = 12;
    private int page = 1;
    private int totalPage = 0;
    
    
    private List<ShowVolunteerBean.DataBean.ListBean> mDatas;
    private ShowVolunteerAdapter mAdapter;
    private String cid;


    public static ShowVolunteerFragment getInstance(String cid){
        ShowVolunteerFragment fragment = new ShowVolunteerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cid",cid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_show_activity;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(mContext,3);
        mXrcv_show.addItemDecoration(new MarginDecoration(mXrcv_show,getResources().getDimensionPixelOffset(R.dimen.layout_padding_small),3));
        mXrcv_show.setLayoutManager(manager);
        mXrcv_show.setPullRefreshEnabled(true);
        mXrcv_show.setLoadingMoreEnabled(true);
        mAdapter = new ShowVolunteerAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_project);//item_show_activity
    }

    @Override
    protected void initEvents() {

        mXrcv_show.setAdapter(mAdapter);
        mXrcv_show.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void getDatas(Bundle arguments) {
        if (arguments != null){
            cid = arguments.getString("cid");
            if (cid != null){
                getList();
            }
        }
    }

    //    @Override
    //    protected void loadDatas() {
    //        getList();
    //    }

    /**
     * 获取分类获取内容列表
     */
    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("volunteer", RequestFactory.getInstance().volunteer(page+"",limit+"",cid), new OnResponeListener<ShowVolunteerBean>() {
            @Override
            public void onSuccess(ShowVolunteerBean bean) {
                Logger.d("volunteer==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_show.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ShowVolunteerBean.DataBean.ListBean empty = new ShowVolunteerBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_show.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_show.refreshComplete();
                } else {
                    mXrcv_show.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("volunteer==>>", error);
                if (isRefresh) {
                    mXrcv_show.refreshComplete();
                } else {
                    mXrcv_show.loadMoreComplete();
                }
            }
        });
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_show.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            getList();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).volunteerId);
        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,mDatas.get(position).title);
        bundle.putString(Constants.KEY.NEWS.HREF,Constants.URL.IP + "/volunteer/detail/" + mDatas.get(position).volunteerId);
//        bundle.putString(Constants.KEY.NEWS.HREF,"http://test253.maoming.gov.cn/news/detail/1001");
        startActivity(NewsDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
